import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * Created by Administrator on 2016/7/9.
 */
public class Task implements Runnable {
    private LinkedBlockingQueue<String> jobs;

    public Task(LinkedBlockingQueue<String> jobs) {
        this.jobs = jobs;
    }

    @Override
    public void run() {
        while (true) {
            repeate();
        }
    }

    public void addJob(String jobName) throws InterruptedException {
        jobs.offer(jobName);
    }

    private void repeate() {
        String jobName = jobs.poll();
        if (jobName != null) {
            if (new Random().nextBoolean()) {
                System.out.println(new Date() + jobName);
            }else {
                System.out.println("add" + jobName);
                try {
                    addJob(jobName);
                } catch (InterruptedException e) {
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        } else {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    }
}
